package com.jubotech.framework.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author xiaoxx
 * @date 2019/10/23 19:41
 */
public class DateFormatUtils {

    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String STANDARD_FORMAT1 = "yyyy-MM-dd";

    public static final String STANDARD_FORMAT2 = "yyyyMMdd";

    public static final String STANDARD_FORMAT3 = "yyyyMMddHH";

    public static final String STANDARD_FORMAT4 = "yyyy年MM月dd日";

    public static final String STANDARD_FORMAT5 = "HH:mm:ss";

    public static final String STANDARD_FORMAT6 = "yyyy.MM.dd";

    public static final String STANDARD_FORMAT7 = "yyyy-MM-dd'T'HH:mm:ss.SSS";

    public static final String STANDARD_FORMAT8 = "yyyy-MM-dd'T'HH:mm:ss";

    public static final String STANDARD_FORMAT9 = "yyyyMMddHHmmss";

    /**
     * 将时间转换为字符串
     * @param date
     * @param formatStr
     * @return
     */
    public static String dateToStr(Date date, String formatStr){
        if(date == null){
            return null;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

    /**
     * 将字符串转换为指定格式的时间
     * @param dateTimeStr 要格式化的数据
     * @param formatType 格式
     * @return
     */
    public static Date strToDate(String dateTimeStr,String formatType){
        if(StringUtils.isEmpty(dateTimeStr)){
            return null;
        }
        org.joda.time.format.DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatType);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    /**
     * 将localDateTime类型的时间转换为指定格式的字符串
     * @param localDateTime 本地时间
     * @param formatType 转换类型
     * @return 时间
     */
    public static String format(LocalDateTime localDateTime,String formatType){
        return localDateTime.format(DateTimeFormatter.ofPattern(formatType));
    }

    public static String format(long time){
        String pattern = "yyyy.MM.dd";
        DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern(pattern);
        return timeFormat.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
    }

    /**
     * 获取当前时间的20分钟前，用来订单查询
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getTwentyMinutesTime() {
        return LocalDateTime.now().plusMinutes(-20).format(DateTimeFormatter.ofPattern(STANDARD_FORMAT));
    }

    public static LocalDateTime getTwentyMinutesDateTime(int minutes) {
        return LocalDateTime.now().plusMinutes(minutes);
    }

    public static LocalDateTime getTwentyOldDaysMinutesDateTime(int minutes) {
        return LocalDateTime.now().plusDays(-1).plusMinutes(minutes);
    }

    /**
     * 获取当前时间的3小时前
     * @return
     */
    public static String getThreeHoursTime(){
        return LocalDateTime.now().plusHours(-3).format(DateTimeFormatter.ofPattern(STANDARD_FORMAT));
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String getThisTime(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(STANDARD_FORMAT));
    }

    /**
     * 获取时间
     * @param minutes
     * @return
     */
    public static LocalDateTime getDateTime(int minutes) {
        return LocalDateTime.now().plusMinutes(minutes);
    }

    /**
     * 获取秒数
     * @param minutes 分钟
     * @return 结果
     */
    public static Long getDateTimeToSecond(int minutes) {
        return getDateTime(minutes).toEpochSecond(ZoneOffset.of("+8"));
    }

    public static LocalDateTime getDateTimeByDayAndMinutes(int day, int minutes) {
        return LocalDateTime.now().plusDays(day).plusMinutes(minutes);
    }

    public static Long getDateTimeToSecond(int day, int minutes) {
        return getDateTimeByDayAndMinutes(day,minutes).toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 获取毫秒数
     * @param minutes 分钟
     * @return 结果
     */
    public static Long getDateTimeToMilliSecond(int minutes) {
        return getDateTime(minutes).toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static LocalDateTime getThreeHoursDateTime(){
        return LocalDateTime.now().plusHours(-3);
    }

    public static LocalDateTime getThisDateTime(){
        return LocalDateTime.now();
    }

    /**
     * 判断是不是今天
     * @param s
     * @param s2
     * @return
     */
    public static Boolean isToDay(Date s , Date s2) {

        return DateUtils.isSameDay(s, s2);
    }


    /***
     * 获取系统时间和传入一个String类型的时间进行转换为特定格式
     * @param d
     * @return
     */
    @SuppressWarnings("deprecation")
    public static String getTAOKETime(String d, String formatType) {
        /**yyyyMd yyyyMMddHHmm yyyyMMddHH*/
        SimpleDateFormat df = new SimpleDateFormat(formatType);
        if (d == null) {
            return df.format(System.currentTimeMillis());
        } else if ("jdHH".equals(d)) {//jdHH代表京东查询按照小时
            return df.format(System.currentTimeMillis());
        } else if ("jdMM".equals(d)) {//jdHH代表京东查询分钟级
            return df.format(System.currentTimeMillis());
        } else {
            return df.format(new Date(d));
        }
    }

    /**
     * TODO 获取当天的开始时间时间戳
     *
     * @return
     */
    public static Long getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime().getTime();
    }

    /***
     * TODO 获取当天的结束时间时间戳
     * @return
     */
    public static Long getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime().getTime();
    }

    /**
     * TODO 获取当前时间的20分钟前，用来订单查询
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getSystemTime() {
        SimpleDateFormat df = new SimpleDateFormat(STANDARD_FORMAT);
        Calendar beforeTime = Calendar.getInstance();
        //获取20分钟前的时间
        beforeTime.add(Calendar.MINUTE, -20);
        Date beforeD = beforeTime.getTime();
        return df.format(beforeD);
    }


}


